class ListNode {
    val: number
    next: ListNode | null
    constructor(val?: number, next?: ListNode | null) {
        this.val = (val===undefined ? 0 : val)
    this.next = (next===undefined ? null : next)
    }
}
const f = new ListNode(6,null)
const e = new ListNode(5,f)
const d = new ListNode(4,e)
const c = new ListNode(3,d)
const b = new ListNode(2,c)
const a = new ListNode(1,b)
function middleNode(head: ListNode | null): ListNode | null {
    const arr: (ListNode|null)[] = [head]
    const len: ListNode | number = get(head,1,arr)
    let index:number = 0
    if(len as number %2 === 0){
        index = len as number/2 + 1
    }else {
        index = (len as number+1)/2
    }
    return arr[index-1]
};

function get(head: ListNode | null, index: number, arr:(ListNode|null)[]): ListNode | number {
    if(head?.next){
        arr.push(head.next)
        return get(head.next,index+1,arr)
    }else{
        return index
    }
}

console.log(middleNode(a))